package com.wust.supermarket.controller;

import com.wust.supermarket.domain.Merch;
import com.wust.supermarket.domain.Response;
import com.wust.supermarket.service.MerchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class MerchController {
    @Autowired
    private MerchService merchService;
    @RequestMapping(value = "/merch/listMerch",method = RequestMethod.GET)
    public String ListMerch(Model model) {
        List<Merch> merches=merchService.listAllMerches();
        model.addAttribute("merches", merches);
        List<Merch> merches1=merchService.listAllOffMerchs();
        model.addAttribute("merches1", merches1);
        return "merch/listMerch";
    }

    @RequestMapping(value = "/merch/addMerch",method = RequestMethod.GET)
    public String AddMerch() {
        return "merch/addMerch";
    }

    @PostMapping("/merch/update")
    public Response updateUser(@RequestBody  Merch merch){
        System.out.println(merch);
        if (merchService.updateMerch(merch))
            return new Response<String>(1, "update merch success", null);
        return new Response<String>(1, "update merch failed", null);
    }

    @GetMapping("/merch/delete/{id}")
    @ResponseBody
    public Response deleteMerchByid(@PathVariable("id") int MerchId) {
        int i=merchService.deleteMerch(MerchId);
        if(i>0)
            return new Response<String>(1, "delete merch success", null);
        return new Response<String>(-1, " delete merch failed", null);

    }

    @GetMapping("/merch/getMerchByid/{id}")
    @ResponseBody
    public Merch getMerchByid(@PathVariable("id") int MerchId) {
        Merch merch=merchService.getMerchById(MerchId);
        if(merch==null)
            return null;
        return merch;
    }

    @PostMapping("/merch/add")
    public Response addMerch(@RequestBody  Merch merch){
        System.out.println(merch);
        if (merchService.insertMerch(merch)>0)
            return new Response<String>(1, "add merch success", null);
        return new Response<String>(1, "add merch failed", null);
    }

    
}
